package edu.neu.cs6515.xml.converter;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import edu.neu.cs6515.xml.IngeniousColor;
import edu.neu.cs6515.xml.Tile;

public class TileConverter implements Converter {
    @Override
    public boolean canConvert(final Class type) {
        return Tile.class.equals(type);
    }

    @Override
    public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
        Tile t = (Tile) source;
        writer.addAttribute("c0", t.getC0().toString());
        writer.addAttribute("c1", t.getC1().toString());
    }

    @Override
    public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
        String c0 = reader.getAttribute("c0");
        String c1 = reader.getAttribute("c1");
        Tile tile = new Tile(IngeniousColor.valueOf(c0),
                IngeniousColor.valueOf(c1));
        return tile;
    }

}
